package com.orphan.limitedDeal.controller;

import com.orphan.limitedDeal.po.PromotionGoodss;
import com.orphan.limitedDeal.service.impl.PromotionServiceImpl;
import com.orphan.util.dto.PromotionGoodsDto;
import com.orphan.util.mqConfig.PromotionMq;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
@RestController
@RequestMapping("/promotion")
public class PromotionController {
    @Autowired
    PromotionServiceImpl promotionService;
    @Autowired
    RabbitTemplate rabbitTemplate;
    @GetMapping("/getGoods")
    public List<PromotionGoodss> selectAll(){
        List<PromotionGoodss> promotionGoodssList = promotionService.selectAll();
        System.out.println(promotionGoodssList);
        return promotionGoodssList;
    }
    @PostMapping("/setTag")
    public boolean setTag(String goodsId){
        PromotionGoodss promotionGoodss=new PromotionGoodss();
        promotionGoodss.setGoodsId(Integer.parseInt(goodsId));
        boolean a= promotionService.setTag(promotionGoodss);
        System.out.println("a = " + a);
        return a;
    }
    @PostMapping("/updateGoods")
    public boolean updateGoods(String goodsId,String goodsNum,String goodsName,String goodsType,String goodsPrice,String promotion,String goodsTag){
        PromotionGoodss promotionGoodss=new PromotionGoodss();
        PromotionGoodsDto promotionGoodsDto=new PromotionGoodsDto();
        promotionGoodss.setGoodsId(Integer.parseInt(goodsId));
        promotionGoodss.setGoodsNum(Integer.parseInt(goodsNum));
        promotionGoodss.setGoodsName(goodsName);
        promotionGoodss.setGoodsType(goodsType);
        promotionGoodss.setGoodsTag(Integer.parseInt(goodsTag));
        promotionGoodss.setPromotion(Float.parseFloat(promotion));
        System.out.println(promotionGoodss.getPromotion());

        promotionGoodsDto.setGoodsId(Integer.parseInt(goodsId));
        promotionGoodsDto.setGoodsNum(Integer.parseInt(goodsNum));
        promotionGoodsDto.setGoodsName(goodsName);
        promotionGoodsDto.setGoodsType(goodsType);
        promotionGoodsDto.setGoodsPrice(goodsPrice);
        promotionGoodsDto.setGoodsDiscount(Float.parseFloat(promotion));
//        promotionGoodsDto.setGoodsTag(Integer.parseInt(goodsTag));
        System.out.println("添加"+promotionGoodsDto);

        rabbitTemplate.convertAndSend("directExchangeProm","Promotion",promotionGoodsDto);
        boolean a= promotionService.updateGoods(promotionGoodss);
        System.out.println("a = " + a);
        return a;
    }
    @PostMapping("/selectGoodsName")
    public List<PromotionGoodss> selectGoodsName(String goodsName){
        PromotionGoodss promotionGoodss =new PromotionGoodss();
        promotionGoodss.setGoodsName(goodsName);
        System.out.println(promotionGoodss.getGoodsName());
        return promotionService.selectGoodsName(goodsName);
    }
    @GetMapping("/selectPromotion")
    public List<PromotionGoodss> selectPromotion(){
        List<PromotionGoodss> promotionGoodssList = promotionService.selectPromotion();
        return promotionGoodssList;
    }

}
